{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一个线性回归模型\n",
    "\n",
    "一个直线 y = wx+b ,在这个线周围生成一些数据点，然后用TF对这些点做回归，求w和b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x1c5d8ac92b0>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成数据\n",
    "num_points = 1000\n",
    "vectors = []\n",
    "for i in range(num_points):\n",
    "    x = np.random.normal(0.5,1.5)\n",
    "    y = 0.1*x + 0.3 + np.random.normal(0.02,0.03)\n",
    "    vectors.append([x,y])\n",
    "\n",
    "# generate some samples\n",
    "x_data = [v[0] for v in vectors]\n",
    "y_data = [v[1] for v in vectors]\n",
    "\n",
    "plt.scatter(x_data,y_data,c='r')\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W: [ 0.46905804] b: [ 0.] loss:  0.339243\n",
      "update step:  0 W: [ 0.45302656] b: [ 0.0026527] loss:  0.313472\n",
      "update step:  1 W: [ 0.43780398] b: [ 0.00541417] loss:  0.290106\n",
      "update step:  2 W: [ 0.423347] b: [ 0.00827408] loss:  0.268899\n",
      "update step:  3 W: [ 0.40961474] b: [ 0.01122272] loss:  0.249631\n",
      "update step:  4 W: [ 0.39656854] b: [ 0.014251] loss:  0.232107\n",
      "update step:  5 W: [ 0.38417178] b: [ 0.01735041] loss:  0.216149\n",
      "update step:  6 W: [ 0.37238994] b: [ 0.02051297] loss:  0.201602\n",
      "update step:  7 W: [ 0.36119032] b: [ 0.02373121] loss:  0.188324\n",
      "update step:  8 W: [ 0.35054201] b: [ 0.02699813] loss:  0.176189\n",
      "update step:  9 W: [ 0.34041578] b: [ 0.03030721] loss:  0.165085\n",
      "update step:  10 W: [ 0.33078396] b: [ 0.03365231] loss:  0.154909\n",
      "update step:  11 W: [ 0.32162046] b: [ 0.03702775] loss:  0.145572\n",
      "update step:  12 W: [ 0.31290054] b: [ 0.04042817] loss:  0.136992\n",
      "update step:  13 W: [ 0.30460083] b: [ 0.0438486] loss:  0.129096\n",
      "update step:  14 W: [ 0.29669923] b: [ 0.04728441] loss:  0.121819\n",
      "update step:  15 W: [ 0.28917482] b: [ 0.05073126] loss:  0.115102\n",
      "update step:  16 W: [ 0.28200784] b: [ 0.05418513] loss:  0.108894\n",
      "update step:  17 W: [ 0.27517959] b: [ 0.05764227] loss:  0.103146\n",
      "update step:  18 W: [ 0.26867238] b: [ 0.06109919] loss:  0.0978158\n",
      "update step:  19 W: [ 0.26246944] b: [ 0.06455266] loss:  0.0928661\n",
      "update step:  20 W: [ 0.25655499] b: [ 0.06799967] loss:  0.0882623\n",
      "update step:  21 W: [ 0.25091404] b: [ 0.07143744] loss:  0.0839736\n",
      "update step:  22 W: [ 0.24553242] b: [ 0.0748634] loss:  0.0799723\n",
      "update step:  23 W: [ 0.24039674] b: [ 0.07827517] loss:  0.0762334\n",
      "update step:  24 W: [ 0.23549432] b: [ 0.08167054] loss:  0.0727346\n",
      "update step:  25 W: [ 0.23081318] b: [ 0.08504749] loss:  0.0694555\n",
      "update step:  26 W: [ 0.22634195] b: [ 0.08840416] loss:  0.0663778\n",
      "update step:  27 W: [ 0.2220699] b: [ 0.09173882] loss:  0.0634851\n",
      "update step:  28 W: [ 0.21798688] b: [ 0.09504991] loss:  0.0607624\n",
      "update step:  29 W: [ 0.21408327] b: [ 0.098336] loss:  0.0581963\n",
      "update step:  30 W: [ 0.21034996] b: [ 0.10159577] loss:  0.0557746\n",
      "update step:  31 W: [ 0.20677836] b: [ 0.10482802] loss:  0.0534862\n",
      "update step:  32 W: [ 0.2033603] b: [ 0.10803169] loss:  0.0513213\n",
      "update step:  33 W: [ 0.20008808] b: [ 0.11120579] loss:  0.0492705\n",
      "update step:  34 W: [ 0.19695438] b: [ 0.11434943] loss:  0.0473258\n",
      "update step:  35 W: [ 0.19395229] b: [ 0.11746184] loss:  0.0454796\n",
      "update step:  36 W: [ 0.19107528] b: [ 0.1205423] loss:  0.0437251\n",
      "update step:  37 W: [ 0.18831713] b: [ 0.12359019] loss:  0.042056\n",
      "update step:  38 W: [ 0.185672] b: [ 0.12660496] loss:  0.0404667\n",
      "update step:  39 W: [ 0.1831343] b: [ 0.12958615] loss:  0.038952\n",
      "update step:  40 W: [ 0.1806988] b: [ 0.13253331] loss:  0.0375071\n",
      "update step:  41 W: [ 0.17836049] b: [ 0.13544613] loss:  0.0361278\n",
      "update step:  42 W: [ 0.17611468] b: [ 0.13832429] loss:  0.0348099\n",
      "update step:  43 W: [ 0.17395687] b: [ 0.14116757] loss:  0.0335499\n",
      "update step:  44 W: [ 0.17188284] b: [ 0.14397575] loss:  0.0323444\n",
      "update step:  45 W: [ 0.16988857] b: [ 0.14674871] loss:  0.0311902\n",
      "update step:  46 W: [ 0.16797027] b: [ 0.14948633] loss:  0.0300846\n",
      "update step:  47 W: [ 0.16612433] b: [ 0.15218857] loss:  0.0290248\n",
      "update step:  48 W: [ 0.16434734] b: [ 0.1548554] loss:  0.0280083\n",
      "update step:  49 W: [ 0.16263604] b: [ 0.15748683] loss:  0.0270331\n"
     ]
    }
   ],
   "source": [
    "# init W, b\n",
    "W = tf.Variable(tf.random_uniform([1],-1.,1.),name='W')\n",
    "b = tf.Variable(tf.zeros([1]),name='b')\n",
    "y = W*x_data+b\n",
    "loss = tf.reduce_mean(tf.square(y-y_data))\n",
    "\n",
    "optim = tf.train.GradientDescentOptimizer(0.01)\n",
    "train = optim.minimize(loss,name='train')\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    init = tf.global_variables_initializer()\n",
    "    sess.run(init)\n",
    "    print ('W:',sess.run(W),'b:',sess.run(b),'loss: ',sess.run(loss))\n",
    "    for step in range(50):\n",
    "        sess.run(train)\n",
    "        print ('update step: ',step,'W:',sess.run(W),'b:',sess.run(b),'loss: ',sess.run(loss))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
